<?php

namespace Kanin\Games\lib;

use Kanin\MyTools\KMongo;
use Swoole\Database\RedisConfig;
use Swoole\Database\RedisPool;

class Tool {

	private static $_static = [];

	/**
	 * @param $name
	 * @return \Swoole\Database\RedisPool
	 */
	public static function redisPool($name): RedisPool {
		if (!isset($_static['redisPool'][$name])) {
			$config = Config::get('redis.' . $name);
			$RedisConfig = (new RedisConfig)
				->withHost($config['host'])
				->withPort($config['port'])
				->withAuth($config['auth'])
				->withDbIndex(0)
				->withTimeout(1);
			$_static['redisPool'][$name] = new RedisPool($RedisConfig);
		}
		return  $_static['redisPool'][$name];
	}

	/**
	 * mongo实例化
	 * @param $name
	 * @return \Kanin\MyTools\KMongo
	 */
	public static function mongo($name) {
		if (!isset($_static['mongo'][$name])) {
			$config = Config::get('mongo.' . $name);
			$mongo = new KMongo($config['host'], $config['port'], $config['username'], $config['password']);
			self::$_static['mongo'][$name] = $mongo;
		}
		return self::$_static['mongo'][$name];
	}

	/**
	 * 模型实例化
	 * @param $name
	 * @return mixed
	 */
	public static function model($name) {
		if (!isset($_static['model'][$name])) {
			$class = "Kanin\\Games\\model\\$name";
			$mongo = new $class();
			self::$_static['model'][$name] = $mongo;
		}
		return self::$_static['model'][$name];
	}
}